package com.qf.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.sys.po.Dept;
import com.qf.sys.service.DeptService;
import com.qf.sys.util.LayUIOperate;
import com.qf.sys.util.Msg;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建人 sunsir Email:sunsir163@163.com
 * @创建时间 2022/5/7
 * @描述
 */
@Controller
@RequestMapping("dept")
public class DeptController {
    @Resource
    DeptService deptService;

    @RequestMapping("selectDeptList")
    @ResponseBody
    public Map selectDeptList(){//查询部门信息
        Map<String,Object> maps = new HashMap<>();
        List<Dept> deptList = deptService.selectDeptList();//查询出数据
        System.out.println(deptList);
        maps.put("data",deptList);
        return maps;
    }
    @RequestMapping("getDeptList")
    @ResponseBody
    public Msg getDeptList(HttpServletRequest request){
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String deptName = request.getParameter("deptName");
        String _cno = request.getParameter("cno");
        int cno = 0;
        if (_cno != null && !"".equals(_cno)) {
            cno = Integer.parseInt(_cno);
        }
        String _pno = request.getParameter("pno");
        int pno = 0;
        if (_pno != null && !"".equals(_pno)) {
            pno = Integer.parseInt(_pno);
        }
        String _status = request.getParameter("status");
        System.out.println(_status);
        int status = 3;
        if (_status != null && !"".equals(_status)) {
            status = Integer.parseInt(_status);
        }
        Map params = new HashMap();
        params.put("cno", cno);
        params.put("pno", pno);
        params.put("deptName", deptName);
        params.put("status", status);
        System.out.println(status);
        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<Dept> data = deptService.getDeptList(params);
        Msg msg = new Msg();
        msg.setCode(0);
        msg.setMsg("处理成功");
        msg.setCount(data.getTotal());//得到总条数
        msg.setData(data.getList());//得到查出的List数据
        System.out.println(msg);
        return msg;
    }
    @RequestMapping("addDept")
    @ResponseBody
    public LayUIOperate addDept(@RequestBody Map<String,Object> params){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("addDept -->"+params);
        boolean f= deptService.addDept(params);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户添加成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户添加失败");
        }
        return operate;
    }
    @RequestMapping("updateDept")
    @ResponseBody
    public LayUIOperate updateDept(@RequestBody Map<String,Object> params){
        LayUIOperate operate=new LayUIOperate();
        System.out.println("updateDept -->"+params);
        boolean f= deptService.updateDept(params);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户修改成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户修改失败");
        }
        return operate;
    }

    @RequestMapping("deleteDept")
    @ResponseBody
    public LayUIOperate deleteDept(Integer id){
        LayUIOperate operate=new LayUIOperate();
        boolean f= deptService.deleteDept(id);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("部门删除成功！");
        }else{
            operate.setSuccess(false);
            operate.setMessage("部门删除失败！");
        }
        return operate;
    }

    @RequestMapping("upadteDeptstatusTo0")
    @ResponseBody
    public LayUIOperate upadteDeptstatusTo0(int deptNo){
        LayUIOperate operate=new LayUIOperate();
        boolean f= deptService.upadteDeptstatusTo0(deptNo);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户删除成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户删除失败");
        }
        return operate;
    }
    @RequestMapping("upadteDeptstatusTo1")
    @ResponseBody
    public LayUIOperate upadteDeptstatusTo1(int deptNo){
        LayUIOperate operate=new LayUIOperate();
        boolean f= deptService.upadteDeptstatusTo1(deptNo);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("用户删除成功！牛逼");
        }else{
            operate.setSuccess(false);
            operate.setMessage("用户删除失败");
        }
        return operate;
    }
}
